﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "GbmpJoinCutInstanceUtils.h"
#include "IInstance.h"
#include "IElementBasicInformation.h"
#include "UniIdentity.h"

#include "GbmpWallUtils.h"
#include "GbmpBeamUtils.h"
#include "GbmpColumnUtils.h"
#include "GbmpStripFoundationUtils.h"

#include "IGroupType.h"
#include "IDocument.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"
using namespace gcmp;

bool gcmp::GbmpJoinCutInstanceUtils::IsAllowJoin(const IElement* pElem)
{
    if (pElem == nullptr)
    {
        return false;
    }
    return GbmpWallUtils::IsWall(pElem) || GbmpBeamUtils::IsBeam(pElem) || GbmpStripFoundationUtils::IsStripFoundation(pElem);
}

bool gcmp::GbmpJoinCutInstanceUtils::IsJoinable(const IInstance* pInstance)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pInstance, L"invalid pInstance",L"GDMPLab",L"2024-03-30");
    if (GbmpWallUtils::IsWall(pInstance))
    {
        return GbmpWallUtils::IsJoinable(pInstance);
    }
    else if (GbmpBeamUtils::IsBeam(pInstance))
    {
        return GbmpBeamUtils::IsJoinable(pInstance);
    }
    else if (GbmpColumnUtils::IsColumn(pInstance))
    {
        return GbmpColumnUtils::IsJoinable(pInstance);
    }
    else if (GbmpStripFoundationUtils::IsStripFoundation(pInstance))
    {
        return GbmpStripFoundationUtils::IsJoinable(pInstance);
    }

    DBG_WARN(L"试图操作不支持的构件",L"GDMPLab",L"2024-03-30");
    return false;
}

bool gcmp::GbmpJoinCutInstanceUtils::IsJoinable(const IInstance* pInstance1, const IInstance* pInstance2)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pInstance1, L"invalid pInstance1",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pInstance2, L"invalid pInstance2",L"GDMPLab",L"2024-03-30");
    if (!IsJoinable(pInstance1) || !IsJoinable(pInstance2))
    {
        return false;
    }

    // 组类型中的元素只能和该组类型的元素建立连接关系
    const IDocument* pDoc = pInstance1->GetDocument();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pDoc, L"invalid pDoc",L"GDMPLab",L"2024-03-30");
    const IGroupType* pGroupType1 = quick_cast<const IGroupType>(pDoc->GetElement(pInstance1->GetGroupId()));
    const IGroupType* pGroupType2 = quick_cast<const IGroupType>(pDoc->GetElement(pInstance2->GetGroupId()));
    if (pGroupType1 != pGroupType2)
        return false;

    //只允许类型相同的进行连接
    if (pInstance1->GetBasicInformation()->GetCategoryUid() != pInstance2->GetBasicInformation()->GetCategoryUid())
    {
        return false;
    }

    return true;
}

double gcmp::GbmpJoinCutInstanceUtils::GetWidth(const IInstance* pInstance)
{
    DBG_WARN_AND_RETURN_UNLESS(pInstance, 0, L"invalid pInstance",L"GDMPLab",L"2024-03-30");
    if (GbmpWallUtils::IsWall(pInstance))
    {
        return GbmpWallUtils::GetWallThickness(pInstance);
    }
    else if (GbmpBeamUtils::IsBeam(pInstance))
    {
        return GbmpBeamUtils::GetBeamWidth(pInstance);
    }
    else if (GbmpColumnUtils::IsColumn(pInstance))
    {
        return GbmpColumnUtils::GetColumnWidth(pInstance);
    }
    else if (GbmpStripFoundationUtils::IsStripFoundation(pInstance))
    {
        return GbmpStripFoundationUtils::GetStripFoundationWidth(pInstance);
    }
    DBG_WARN(L"试图操作不支持的构件",L"GDMPLab",L"2024-03-30");
    return 0;
}

double gcmp::GbmpJoinCutInstanceUtils::GetHeight(const IInstance* pInstance)
{
    DBG_WARN_AND_RETURN_UNLESS(pInstance, 0, L"invalid pInstance",L"GDMPLab",L"2024-03-30");
    if (GbmpWallUtils::IsWall(pInstance))
    {
        return GbmpWallUtils::GetWallHeight(pInstance);
    }
    else if (GbmpBeamUtils::IsBeam(pInstance))
    {
        return GbmpBeamUtils::GetBeamHeight(pInstance);
    }
    else if (GbmpColumnUtils::IsColumn(pInstance))
    {
        return GbmpColumnUtils::GetColumnHeight(pInstance);
    }
    else if (GbmpStripFoundationUtils::IsStripFoundation(pInstance))
    {
        return GbmpStripFoundationUtils::GetStripFoundationHeight(pInstance);
    }
    DBG_WARN(L"试图操作不支持的构件",L"GDMPLab",L"2024-03-30");
    return 0;
}
